/**
 * description
 * <p>fill description</p>
 * @author zido
 * @since 2017/5/30 0030
 */

import {applyMiddleware,compose,createStore} from 'redux'
import thunk from 'redux-thunk'
import { browserHistory } from 'react-router'
import makeRootReducer from './reducers'
import {updateLocation} from './location'

const isDev = (process.env.NODE_ENV === 'development')
export default (initialState = {}) => {
  const middleware = [thunk]

  const enhancers = []
  let composeEnhancers = compose
  if(isDev){
    const devToolsExtension = window.devToolsExtendsion
    if(typeof devToolsExtension === 'function') {
      enhancers.push(devToolsExtension())
    }
  }

  const store = createStore(
    makeRootReducer(),
    initialState,
    composeEnhancers(
      applyMiddleware(...middleware),
      ...enhancers
    )
  )
  store.asyncReducers = {}

  store.unsubscribeHistory = browserHistory.listen(updateLocation(store))

  if(module.hot){
    module.hot.accept('./reducers',()=>{
      const reducers = require('./reducers').default
      store.replaceReducer(reducers(store.asyncReducers))
    })
  }

  return store
}